#include "RingQueue.hpp"
#include "Task.hpp"

using namespace std;

void *Productor(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *> (args);

    while(true)
    {
        // 生产
        int data1 = rand() % 10;
        usleep(rand() % 123);
        int data2 = rand() % 10;
        usleep(rand() % 123);
        char oper = opers[rand() % opers.size()];

        Task t(data1, data2, oper);
        rq->Push(t);
        cout << "product task: " << t.PrintTask() << endl;


        sleep(1);
    }

    return nullptr;
}

void *Consumer(void *args)
{
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *> (args);

    while(true)
    {
        Task t;
        rq->Pop(&t);

        t.Run();
        cout << "consumer task: " << t.PrintResult() << endl;

    }

    return nullptr;
}

int main()
{
    srand((uint64_t)time(nullptr) ^ getpid() ^ pthread_self());

    pthread_t c, p;
    RingQueue<Task> *rq = new RingQueue<Task> ();

    pthread_create(&p, nullptr, Productor, rq);
    pthread_create(&c, nullptr, Consumer, rq);

    pthread_join(p, nullptr);
    pthread_join(c, nullptr);
    return 0;
}